package com.hspedu.mhl.service;

/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2023/04/07/9:12
 */

import com.hspedu.mhl.dao.BillDAO;
import com.hspedu.mhl.dao.DiningTableDAO;
import com.hspedu.mhl.dao.MultiTableDAO;
import com.hspedu.mhl.domain.Bill;
import com.hspedu.mhl.domain.MultiTableBean;

import java.util.List;
import java.util.UUID;

/**
 * 处理和账单相关的业务
 */
public class BillService {
    //定义一个BillDAO相关的属性
    private BillDAO billDAO = new BillDAO();
    //需要调用menuService的方法，所以定义一个它的属性
    private MenuService menuService = new MenuService();
    //需要DiningTable属性
    private DiningTableService diningTableService = new DiningTableService();
    private DiningTableDAO diningTableDAO = new DiningTableDAO();
    MultiTableDAO multiTableDAO = new MultiTableDAO();

    /**
     * 编写点餐的方法
     *
     * @param menuID
     * @param nums
     * @param diningTable
     * @return 点餐成功返回true，否则返回false
     */
    public boolean orderMenu(int menuID, int nums, int diningTable) {
        //产生随机字符串，不会重复的
        String billID = UUID.randomUUID().toString();
        //将账单生成到bill表
        int update = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')",
                billID, menuID, nums, menuService.getMenuById(menuID).getPrice() * nums, diningTable);
        if (update <= 0) {
            return false;
        }
        //需要更新对应餐桌的
        boolean bol = diningTableService.updateDiningTableState(menuID, "就餐中");
        return bol;
    }

    //返回所有的账单
    public void list() {
        System.out.println("menuID" + "\t\t"
                + "nums" + "\t\t"
                + "money" + "\t\t"
                + "diningTable" + "\t\t"
                + "billDate" + "\t\t"
                + "state");
        List<Bill> bills = billDAO.queryMulti("select *from bill", Bill.class);
        for (Bill bill : bills) {
            System.out.println(bill);
        }
    }

    //返回多表查询的结果
    public void list2() {
        System.out.println("menuID" + "\t\t"
                + "nums" + "\t\t"
                + "money" + "\t\t"
                + "diningTable" + "\t\t"
                + "billDate" + "\t\t"
                + "state" + "\t\t"
                + "name" + "\t\t"
                + "price(单价)");
        List<MultiTableBean> multiTableBeans = multiTableDAO.queryMulti("SELECT bill.*,NAME,price as price2 FROM bill,menu WHERE bill.menuId=menu.id", MultiTableBean.class);
        for (MultiTableBean multiTableBean : multiTableBeans) {
            System.out.println(multiTableBean);

        }
    }

    //查看某个餐桌是否有未结账的账单
    public boolean hasPayBillByDiningTableID(int TableId) {
        Bill bill = billDAO.querySingle("SELECT *FROM bill WHERE diningTableId=? AND state ='未结账' LIMIT 0,1", Bill.class, TableId);
        return bill != null;
    }

    //修改bill表的状态，如果餐桌存在，且该餐桌有未结账的账单
    public boolean payBill(int diningTableId, String payMode) {
        //1，修改bill表
        int update = billDAO.update("update bill set state = ? where diningTableId =? and state ='未结账'", payMode, diningTableId);
        if (update <= 0) {
            return false;
        }
        //2。修改diningTable表
        //不要直接在这里操作，调用方法
        if (!diningTableService.updateDiningToFree(diningTableId, "空")) {
            return false;
        }
        return true;
    }
}
